// pages/myOrder/detail.js
const app = getApp()
const req = require('../../utils/req')
const util = require('../../utils/util')
var timer
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.hideShareMenu()
    let that = this
    if(options && options.id && options.type){
        this.setData({
          id:parseInt(options.id),
          type:parseInt(options.type)
        })
        this.getOrderDetail()
    }else{

    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    clearInterval(timer)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getOrderDetail:function(e){
    var that=this
    var id=this.data.id
    var data={}
    data.id=id
    var type=this.data.type
    var url=''
    if(type==1){
      url='/ht/user/userQrOrderDetails'
    }else if(type==2){
      url='/ht/user/userOrderDetails'
    }else{
      wx.showToast({
        title: '参数错误',
        icon:'none',
        duration:1500
      })
      return false
    }
    req.request(url,data,'POST').then(res=>{
      var datas=res.data
      that.setData({
        detail:datas
      })
      if(datas.surplusTime>0){
       
        that.countDown(datas.surplusTime)
      }
    })
  },
  countDown:function(){
    
    var that = this
    var detail=this.data.detail
    timer = setInterval(function () {
      if(detail.surplusTime && detail.surplusTime>0){

        detail.surplusTime=parseInt(detail.surplusTime)-1

        if(detail.surplusTime==0){
          clearInterval(timer)
        }
        detail.ms=util.timeStampToHms(detail.surplusTime)
        console.log(detail.ms)
      }

      that.setData({
        detail:detail
      })
      
      }, 1000);
  },

  cancelOrder:function(e){
    var that=this
    var id=e.currentTarget.dataset.id
    var lists=this.data.lists
    var data={}
    data.id=id
    wx.showModal({
      title: '温馨提示',
      content: '是否确认取消该订单',
      success: function (res) {
        if (res.confirm) {
          req.request('/ht/user/delOrder',data,'POST').then(res=>{

            clearInterval(timer)
            wx.navigateBack()
            
          })
        } else if (res.cancel) {
          console.log('取消删除')
        }
      }
    })

  },
  toPay:function(e){
    var type=this.data.type
    var id=e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/submitDetail/index?id='+id+'&type='+type,
    })
  },
  toStore:function(){
    var detail=this.data.detail
    wx.navigateTo({
      url: '/pages/storeDetail/index?data='+detail.storeId,
    })
  }

})